package fr.pud.client.view;

import java.awt.Color;
import java.util.ArrayList;

import fr.pud.client.model.Route;
import fr.pud.client.model.Routes;

/**
 * Apr 9, 2012 - Projet_Ose.
 * @author Simon Devineau, Adrien Hierry, Leopold Houdin, Pierre Reliquet
 *         Ecole des Mines de Nantes
 *         MapRoutes.java
 *         The MapRoutes represents the routes
 *         {@link fr.pud.client.model.Routes} calculated by the server.
 */
public class MapRoutes {
    /**
     * The single instance of the MapRoutes
     */
    private static MapRoutes instance = null;

    /**
     * @return the single instance of the MapRoutes.
     */
    public static MapRoutes getInstance() {
        if (instance == null) {
            instance = new MapRoutes();
        }
        return instance;
    }

    /**
     * A boolean which indicates the visibility of the MapRoutes. If true, the
     * MapRoutes is visible.
     */
    private boolean             isVisible = true;
    /**
     * The {@link ArrayList} of MapRoute which represents a MapRoutes.
     */
    private ArrayList<MapRoute> routes;

    /**
     * The default constructor.
     */
    private MapRoutes() {
        this.routes = new ArrayList<MapRoute>();
    }

    /**
     * @param r
     *            , the MapRoute to add.
     * @return true if the MapRoute r was added to the list of MapRoute.
     */
    public boolean add(MapRoute r) {
        return this.routes.add(r);
    }

    /**
     * Create and add a MapRoute for each Route of the Routes.
     * @param theRoutes
     *            (a model) to add
     * @return true if each route of the Routes was added to the list of
     *         MapRoutes.
     */
    public boolean add(Routes theRoutes) {
        boolean result = true;
        for (Route route : Routes.getInstance().getRoutes()) {
            MapRoute mapRoute = new MapRoute(route, route.getVehicle(),
                    Color.red);
            result = result && this.add(mapRoute);
        }
        return result;
    }

    /**
     * @param i
     *            , the index of the MapRoute wanted.
     * @return the ieme MapRoute.
     */
    public MapRoute get(int i) {
        return this.routes.get(i);
    }

    /**
     * @return the {@link ArrayList} of MapRoute.
     */
    public ArrayList<MapRoute> getRoutes() {
        return this.routes;
    }

    /**
     * @return the isVisible
     */
    public boolean isVisible() {
        return this.isVisible;
    }

    /**
     * Modify the ieme route by the new Route substitute with the color c.
     * @param index
     *            , the index of the MapRoute to replace.
     * @param substitute
     *            , the Route to substitute.
     * @param c
     *            , the color to draw
     */
    public void setMapRoute(int index, Route substitute, Color c) {
        this.routes.set(index, new MapRoute(substitute,
                substitute.getVehicle(), c));
    }

    /**
     * @param aIsVisible
     *            the isVisible to set
     */
    public void setVisible(boolean aIsVisible) {
        for (MapRoute mapRoute : this.routes) {
            mapRoute.setVisible(aIsVisible);
        }
        JMapViewerExtended.getJMapViewer().setMapRouteVisible(aIsVisible);
        this.isVisible = aIsVisible;
    }
}
